﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyWeb.ScheduledTasks.Business;
using MyWeb.ScheduledTasks.Models;
using MyWeb.ScheduledTasks.Protos;
using MyWeb.ScheduledTasks.Services;
using Quartz.Spi;
using Quartz;

namespace MyWeb.ScheduledTasks.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TaskController : ControllerBase
    {
        private readonly IJobsService _service;
        private readonly ITaskQuartService _taskQuart;

        public TaskController(IJobsService service, ITaskQuartService taskQuart)
        {
            _taskQuart = taskQuart;
            _service = service;
        }
        /// <summary>
        /// 初始化所有作业
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> Init()
        {
            await _taskQuart.InitJobsAsync();
            return ApiResult.SUCCESS();
        }

        /// <summary>
        /// 查询所有作业
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> Get(int page = 1, int pageCount = 2)
        {
            //实体生成数据库表
            //BaseDB.CodeFirst.InitTables(typeof(Jobs));//具体用法看文档迁移
            //return Ok(await _quartz.GetJobs(new Google.Protobuf.WellKnownTypes.Empty(),null));
            return ApiResult.SUCCESS(await _service.GetJobs(page, pageCount));
        }
        /// <summary>
        /// 添加作业任务
        /// </summary>
        /// <param name="jobs"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> AddJob(Jobs jobs)
        {
            return await _taskQuart.AddJob(jobs) ? ApiResult.SUCCESS() : ApiResult.FAILED();
        }
        /// <summary>
        /// 移除任务
        /// </summary>
        /// <param name="jobId">任务Id</param>
        /// <param name="taskName">任务名称</param>
        /// <param name="groupName">任务组名</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> Remove(long jobId = 1671088025716789248, string? taskName = "", string? groupName = "")
        {
            Jobs jobs = new Jobs() { Id = jobId, TaskName = taskName, GroupName = groupName };
            return await _taskQuart.Remove(jobs) ? ApiResult.SUCCESS() : ApiResult.FAILED();
        }
        /// <summary>
        /// 修改任务信息
        /// </summary>
        /// <param name="jobs"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> Update(Jobs jobs)
        {
            return await _taskQuart.Update(jobs) ? ApiResult.SUCCESS() : ApiResult.FAILED();

        }
        /// <summary>
        /// 暂停任务
        /// </summary>
        /// <param name="jobId"></param>
        /// <param name="taskName"></param>
        /// <param name="groupName"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> Pause(long jobId = 1671088025716789248, string? taskName = "", string? groupName = "")
        {
            Jobs jobs = new Jobs() { Id = jobId, TaskName = taskName, GroupName = groupName };
            return await _taskQuart.Pause(jobs) ? ApiResult.SUCCESS() : ApiResult.FAILED();
        }
        /// <summary>
        /// 开始执行任务
        /// </summary>
        /// <param name="jobId"></param>
        /// <param name="taskName"></param>
        /// <param name="groupName"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> Start(long jobId = 1671088025716789248, string? taskName = "", string? groupName = "")
        {
            Jobs request = new Jobs() { Id = jobId, TaskName = taskName, GroupName = groupName };
            return await _taskQuart.Start(request) ? ApiResult.SUCCESS() : ApiResult.FAILED();
        }
        /// <summary>
        /// 运行一次任务
        /// </summary>
        /// <param name="jobId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> Run(long jobId = 1671088025716789248)
        {
            return await _taskQuart.Run(new Jobs() { Id = jobId }) ? ApiResult.SUCCESS() : ApiResult.FAILED();
        }
        /// <summary>
        /// 查看任务状态
        /// </summary>
        /// <param name="jobId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> GetState(long jobId = 1671088025716789248)
        {
            return ApiResult.SUCCESS(await _taskQuart.GetState(new Jobs() { Id = jobId }));
        }
        /// <summary>
        /// 查看任务是否存在
        /// </summary>
        /// <param name="jobId"></param>
        /// <param name="taskName"></param>
        /// <param name="groupName"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> IsQuartzJob(long jobId = 1671088025716789248, string? taskName = "", string? groupName = "")
        {
            Jobs jobs = new Jobs() { Id = jobId, TaskName = taskName, GroupName = groupName };
            return await _taskQuart.IsQuartzJob(jobs) ? ApiResult.SUCCESS() : ApiResult.FAILED();
        }
        /// <summary>
        /// 校验Cron表达式
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        [HttpGet]
        public ApiResult IsValidExpression(string str)
        {
            return _taskQuart.IsValidExpression(str) ? ApiResult.SUCCESS() : ApiResult.FAILED(); 
        }
        [HttpGet]
        public async Task<ApiResult> GetTaskLog(long jobId, int page, int pageSize) => ApiResult.SUCCESS(await _taskQuart.GetLog(jobId, page, pageSize));

        [HttpPost]
        public ApiResult PostTest([FromBody]Jobs jobs)
        {
            return ApiResult.SUCCESS(jobs);
        }

    }
}
